package com.ztom.test

import com.ztom.beans.factory.support.pojo.PropertyValue
import com.ztom.beans.factory.support.pojo.PropertyValues
import com.ztom.beans.factory.support.pojo.BeanDefinition
import com.ztom.beans.factory.support.pojo.BeanReference
import com.ztom.beans.factory.DefaultListableBeanFactory
import com.ztom.test.bean.Car
import com.ztom.test.bean.Person
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

/**
 * @author ZhangTao
 */
class Test03PopulateBeanWithPropertyValues {

    @Test
    fun test() {
        val beanFactory = DefaultListableBeanFactory()

        val carPropertyValues = PropertyValues()
        carPropertyValues.addPropertyValue(PropertyValue("brand", "BMW"))
        val carBeanDefinition = BeanDefinition(Car::class, carPropertyValues)
        beanFactory.registerBeanDefinition("car", carBeanDefinition)

        val propertyValues = PropertyValues()
        propertyValues.addPropertyValue(PropertyValue("name", "无羡"))
        propertyValues.addPropertyValue(PropertyValue("age", 18))
        propertyValues.addPropertyValue(PropertyValue("car", BeanReference("car")))
        val beanDefinition = BeanDefinition(Person::class, propertyValues)
        beanFactory.registerBeanDefinition("person", beanDefinition)

        val bean = beanFactory.getBean("person")
        val person = bean as Person

        assertEquals(person.name, "无羡")
        assertEquals(person.age, 18)
        assertEquals(person.car?.brand, "BMW")
//        person.car?.let { assertEquals(it.brand, "BMW") }
    }
}